//
// Created by daiyizheng on 2022/3/27.
//

#include <vector>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
//反者的中序遍历
void dfs(TreeNode *node);
int currSum= 0;
TreeNode* convertBST(TreeNode* root) {
    dfs(root);
    return root;
}
void dfs(TreeNode *node){
    if (!node)return;
    dfs(node->right);
    //处理当前节点
    node->val+=currSum;
    currSum = node->val;

    dfs(node->left);
}